package com.example.service;

import com.example.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.cluster.lock.support.DelegatingDistributedLock;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.locks.Lock;

/**
 * Created by Administrator on 2017/4/6 0006.
 */
@Service
public class UserService {

    @Autowired
    private RedisService redisService;

    @Autowired
    private RedisTemplate<String, User> redisTemplate;

    public void insertUserInRedis(User user) {
        Lock lock = redisService.getRedisLock();
        try {
            lock.lock();
            redisTemplate.opsForValue().set(user.getUsername(), user);
            System.out.println("success");
        } finally {
            lock.unlock();
        }
    }

    public void insertUserInRedis2(User user) {
        DelegatingDistributedLock lock = redisService.getDelegatingDistributedLock();
        try {
            lock.lock();
            redisTemplate.opsForValue().set(user.getUsername(), user);
            System.out.println("success");
        } finally {
            lock.unlock();
        }
    }

}
